package com.eqdd.yiqidian.ui.companyspace

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.TransactionRecordBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_space.view.*
import java.util.*

/**
 * Created by jhj on 18-7-9.
 */
class TransactionRecordFragment : Fragment() {

    var pageNum = 0
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_space, container, false)
        setView(view)
        return view
    }

    private fun setView(view: View) {
        val companyId = arguments?.getInt(Config.COMPANY_ID) ?: 0
        val status = arguments?.getInt(Config.STATUS) ?: 0
        view.recyclerView.layoutManager = LinearLayoutManager(activity)
        view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
        val mvcHelper = MVCCoolHelper<List<TransactionRecordBean>>(view.coolRefreshView)
        val list = arrayListOf<TransactionRecordBean>()

        val slimAdapterEx = SlimAdapterEx.create().register<TransactionRecordBean>(R.layout.layout_transaction_record, SlimInjector<TransactionRecordBean> { data, injector ->
            val orderStatus = when {
                data?.status == -2 -> "未付款"
                data?.status == 1 -> "已付款"
                else -> null
            }

            injector?.text(R.id.tv_order_id, String.format(Locale.CHINA, "订单号：%s", data?.orderCode))
                    ?.text(R.id.tv_pay_amount, String.format(Locale.CHINA, "支付金额：￥%s（%s）", data?.amountMondy, data?.type))
                    ?.text(R.id.tv_function, String.format(Locale.CHINA, "开通功能：%s", data?.orderType))
                    ?.text(R.id.tv_time, String.format(Locale.CHINA, "开通时间：%s", data?.createTime))
                    ?.text(R.id.tv_years, String.format(Locale.CHINA, "开通年限：%s年", data?.years))
                    ?.text(R.id.tv_status, String.format(Locale.CHINA, "订单状态：%s", orderStatus))
                    ?.visibility(R.id.tv_status, if (orderStatus == null) View.GONE else View.VISIBLE)


        }).attachTo(view.recyclerView).updateData(list as List<TransactionRecordBean>?) as SlimAdapterEx<TransactionRecordBean>?


        val dataSource = ModelRx2DataSource<TransactionRecordBean>(ModelRx2DataSource.OnLoadSource<TransactionRecordBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<TransactionRecordBean>>>(HttpConfig.BASE_URL + HttpConfig.TRANSACTION_RECORD)
                    .params("companyId", companyId)
                    .params("orderType", status)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<TransactionRecordBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    }).adapt(FlowableBody<HttpResult<List<TransactionRecordBean>>>())
                    .flatMap({ result ->
                        pageNum = result.page
                        if (page == 1) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        val dataList = if (result.items == null)
                            ArrayList()
                        else
                            result.items as MutableList<TransactionRecordBean>?
                        Flowable.just<MutableList<TransactionRecordBean>>(dataList)
                    })
        }, 10)


        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()


    }

}